using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.Article;


namespace HengTong.ViewModel.Article.SysArticleVms
{
    public partial class SysArticleListVm : BasePagedListVM<SysArticleView, SysArticleSearcher>
    {

        protected override IEnumerable<IGridColumn<SysArticleView>> InitGridHeader()
        {
            return new List<GridColumn<SysArticleView>>{
                this.MakeGridHeader(x => x.Title),
                this.MakeGridHeader(x => x.SubTitle),
                this.MakeGridHeader(x => x.Summary),
                this.MakeGridHeader(x => x.Content),
                this.MakeGridHeader(x => x.Status),
                this.MakeGridHeader(x => x.CategoryName),
                this.MakeGridHeader(x => x.SortOrder),
                this.MakeGridHeader(x => x.IsTop),
                this.MakeGridHeader(x => x.IsRecommend),
                this.MakeGridHeader(x => x.CoverImageId).SetFormat(CoverImageIdFormat),
                this.MakeGridHeader(x => x.ViewCount),
                this.MakeGridHeader(x => x.LikeCount),
                this.MakeGridHeader(x => x.CommentCount),
                this.MakeGridHeader(x => x.FavoriteCount),
                this.MakeGridHeader(x => x.ShareCount),
                this.MakeGridHeader(x => x.ArticleTagMappingsName),
                this.MakeGridHeaderAction(width: 200)
            };
        }
        private List<ColumnFormatInfo> CoverImageIdFormat(SysArticleView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.CoverImageId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.CoverImageId,640,480),
            };
        }


        public override IOrderedQueryable<SysArticleView> GetSearchQuery()
        {
            var query = DC.Set<SysArticle>()
                .CheckContain(Searcher.Title, x=>x.Title)
                .CheckEqual(Searcher.Status, x=>x.Status)
                .CheckEqual(Searcher.CategoryId, x=>x.CategoryId)
                .CheckEqual(Searcher.IsTop, x=>x.IsTop)
                .CheckEqual(Searcher.IsRecommend, x=>x.IsRecommend)
                .CheckWhere(Searcher.SelectedArticleTagMappingsIds,x=>DC.Set<SysArticleTagMapping>().Where(y=>Searcher.SelectedArticleTagMappingsIds.Contains(y.TagId)).Select(z=>z.ArticleId).Contains(x.ID))
                .Select(x => new SysArticleView
                {
				    ID = x.ID,
                    Title = x.Title,
                    SubTitle = x.SubTitle,
                    Summary = x.Summary,
                    Content = x.Content,
                    Status = x.Status,
                    CategoryName = x.Category.CategoryName,
                    SortOrder = x.SortOrder,
                    IsTop = x.IsTop,
                    IsRecommend = x.IsRecommend,
                    CoverImageId = x.CoverImageId,
                    ViewCount = x.ViewCount,
                    LikeCount = x.LikeCount,
                    CommentCount = x.CommentCount,
                    FavoriteCount = x.FavoriteCount,
                    ShareCount = x.ShareCount,
                    ArticleTagMappingsName = x.ArticleTagMappings.Select(y=>y.Tag.TagName).ToSepratedString(null,","),
                    CreateTime = x.CreateTime
                })
                .OrderByDescending(x => x.CreateTime);
            return query;
        }

    }

    public class SysArticleView : SysArticle{
        [Display(Name = "分类名称")]
        public String CategoryName { get; set; }
        [Display(Name = "标签名称")]
        public String ArticleTagMappingsName { get; set; }

    }
}
